#ifndef __MODULE_HEATER_H__
#define __MODULE_HEATER_H__

#include <stdint.h>
#include <stdbool.h>
#include "module_pid.h"
#include "module_heater_port.h"

enum HEATER_EV
{
    HEATER_EV_NULL = 0x0,
    HEATER_EV_STATE,
    HEATER_EV_ERROR,
    HEATER_EV_HEATER_SW,
    HEATER_EV_USER_TEMP,
    HEATER_EV_RT_TEMP,
    HEATER_EV_TOTAL_STATUS,
};

enum HEATER_WORK_STATE
{
    HEATER_WORK_STATE_INIT = 0x0,
    HEATER_WORK_STATE_RUNNING,
    HEATER_WORK_STATE_ARRIVE,
};

enum HEATER_WORK_ERROR
{
    HEATER_WORK_ERROR_OK = 0x0,
    HEATER_WORK_ERROR_INVALID_NTC,
    HEATER_WORK_ERROR_HEAT_TIMEOUT,
};

typedef struct
{
    enum HEATER_EV ev_type;
    uint8_t ch;
    uint8_t reserve[2];
    union
    {
        enum HEATER_WORK_STATE state;
        enum HEATER_WORK_ERROR error;
        bool en_switch;
        int16_t temp;
        uint32_t unuse;
    } data;
} heat_packet_data;
typedef void *heat_cb_data;
typedef void (*heat_cb)(heat_cb_data user_data, const heat_packet_data *packet);

void module_heater_init(heat_cb func, heat_cb_data data);
void module_heater_deinit(void);
void module_heater_poll(void); //该函数要放在循环中轮询

void module_heater_set_en(uint8_t ch, bool en);
void module_heater_set_temp(uint8_t ch, int16_t temp);
void module_heater_complex(uint8_t ch, bool en, int16_t temp);

void module_heater_total_set_en(bool en);
void module_heater_total_set_temp(int16_t temp);
void module_heater_total_complex(bool en, int16_t temp);

int8_t module_heater_get_max_ch(void);
bool module_heater_get_en(uint8_t ch);
int16_t module_heater_get_user_temp(uint8_t ch);
int16_t module_heater_get_rt_temp(uint8_t ch);
enum HEATER_WORK_ERROR module_heater_get_error_status(uint8_t ch);

struct heater_total_status
{
    uint32_t ev_flag;
    int8_t ch;                              // channel index
    enum HEATER_WORK_STATE state;
    enum HEATER_WORK_ERROR error;
    bool en_switch;
    int16_t user_temp;                      // user set temperature
    int16_t user_temp_vol;                 // user_temp mapping voltage
    int16_t rt_temp;                        // real time temperature
    int16_t rt_temp_vol;                   // rt_temp mapping voltage
    PID pid_core;
    uint32_t tick;                          // tick used to timeout timer
};
void module_heater_get_status(uint8_t ch, struct heater_total_status *info);

#endif

